
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtonsRight="tableButtonsRight"
      :dynamicSearchField="placeHolder"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsInitTable="false"
      :dynamicIsColumnDrop="false"
      @changeToggleProp="changeToggleProp"
    />
  </div>
</template>
<script>
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import OneTableTemplate from "@/components/crm/Template/crmonePage";
import Vue from "vue";

export default {
  name: "ClueQuery",
  components: {
    OneTableTemplate
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(to.path, function() {
      next();
    });
  },
  data() {
    let that = this;
    return {
      queryParam: "",
      placeHolder: `客户名称/电话`,
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.importExcel(),
          text: "导入"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          clickEvent: () =>this.export(),
          text: "导出"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.getHistory(),
          text: "一年前归档记录"
        },
      ],
      tableButtonsRight: [
        {
          compKey: "btnKey10001",
          type: "",
          size: "small",
          icon: "el-icon-refresh",
          clickEvent: () => this.resetcur(),
          text: "重置"
        },
        {
          compKey: "btnKey10000",
          type: "primary",
          icon: "el-icon-search",
          size: "small",
          clickEvent: () => this.query(),
          text: "查询"
        }
      ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
              .tableComponents
          : [
              {
                compKey: "compKey8",
                labelName: "线索单号",
                codeField: "serverOrder",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey1",
                labelName: "客户名称",
                codeField: "custName",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey2",
                labelName: "电话",
                codeField: "contactTel",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey10",
                labelName: "意向品牌",
                codeField: "carBrandCode",
                textField: "carBrandCn",
                lookuptype: crmApis.mdmCarBrandQueryByPage,
                component: () =>
                  import("@/components/crm/lookupValue/apiDropList"),
                type: "dropdownList",
                isMust: false,
                isMul: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey3",
                labelName: "意向车系",
                codeField: "inteSeriesId",
                component: () =>
                  import(
                    "@/components/crm/crmEjectWindows/crmIntenCarSeries/index"
                  ),
                type: "propus",
                isMust: false,
                isMul: false,
                emitCode: this.inteSeriesChange,
                changeCode: () => {}
              },
              {
                compKey: "compKey4",
                labelName: "意向级别",
                codeField: "clueLevelCode",
                lookuptype: "LX141",

                component: () =>
                  import("@/components/crm/lookupValue/lookupvalue"),
                type: "dropdownList",
                isMust: false,
                isMul: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey6",
                labelName: "当前处理人",
                codeField: "nextDealUserName",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey7",
                labelName: "建单人",
                codeField: "createdName",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey9",
                labelName: "线索渠道",
                codeField: "cantactWayCode",
                textField: "cantactWayName",
                lookuptype: crmApis.pcDbCantactWayQueryFindAll,
                component: () =>
                  import("@/components/crm/lookupValue/singleDropList"),
                type: "dropdownList",
                isMust: false,
                isMul: false,
                emitCode: () => {},
                changeCode: this.cantactWayChange
              },
              {
                compKey: "cantactWayCode",
                labelName: "一级信息来源",
                codeField: "infoChanMCode",
                textField: "infoChanMName",
                lookuptype: crmApis.pcDbInfoChanMQueryFind,
                changeCodeArray: [{ changCode: "cantactWayCode" }],
                component: () =>
                  import("@/components/crm/lookupValue/secondDropList"), //导入的组件路径
                type: "dropdownList",
                isMust: false,
                isMul: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "infoChanDNameKey",
                labelName: "二级信息来源",
                codeField: "infoChanDName",
                component: () =>
                  import("@/components/crm/textbox/crmTextInput"),
                type: "",
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey11",
                labelName: "分配状态",
                codeField: "assignStatus",
                lookuptype: "SE0068",
                component: () => import("@/components/org/LookupValue"),
                type: "dropdownList",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey16",
                labelName: "单据状态",
                codeField: "status",
                lookuptype: "LX181",
                attribute: "7",
                component: () =>
                  import("@/components/crm/lookupValue/lookupvalue"),
                type: "dropdownList",
                isRequire: false,
                isMust: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey12",
                labelName: "作业状态",
                codeField: "crruNode",
                lookuptype: "LX182",
                component: () => import("@/components/org/LookupValue"),
                type: "dropdownList",
                isMust: false,
                isMul: false,
                emitCode: () => {},
                changeCode: () => {}
              },
              {
                compKey: "compKey14",
                labelName: "建单时间",
                codeField: "createdDateStart,createdDateEnd",
                component: () =>
                  import("@/components/crm/Time/crmdtDatePicker"),
                type: "inputText",
                isMust: false
              },
              {
                compKey: "compKey15",
                labelName: "留资时间",
                codeField: "remainDataDateStart,remainDataDateEnd",
                component: () =>
                  import("@/components/crm/Time/crmdtDatePicker"),
                type: "inputText",
                isRequire: false,
                isMust: false
              }
            ],
      apiConfig: crmApis.clueServerQueryFromDlr,
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
              {
                prop: "serverOrder",
                label: "线索单号",
                showLink: true,
                clickEvent: index => {
                  this.edit(index);
                },
                width: 180,
                align: "center",
                fixed: true
              },
              {
                prop: "custName",
                label: "客户名称",
                width: 100,
                align: "center",
                fixed: true
              },
              {
                prop: "contactTel",
                label: "电话",
                width: 110,
                align: "center",
                fixed: true
              },
              {
                prop: "carBrandName",
                label: "意向品牌",
                width: 100,
                align: "center"
              },
              {
                prop: "carSeriesCn",
                label: "意向车系",
                width: 150,
                align: "center"
              },
              {
                prop: "clueLevelCode",
                label: "意向级别",
                width: 70,
                align: "center"
              },
              {
                prop: "nextDealUserName",
                label: "当前处理人",
                width: 100,
                align: "center"
              },
              {
                prop: "createdName",
                label: "建单人",
                width: 100,
                align: "center"
              },
              {
                prop: "cantactWayName",
                label: "线索渠道",
                width: 120,
                align: "center"
              },
              {
                prop: "infoChanMName",
                label: "一级信息来源",
                width: 120,
                align: "center"
              },
              {
                prop: "infoChanDName",
                label: "二级信息来源",
                width: 120,
                align: "center"
              },
              {
                prop: "assignStatusName",
                label: "分配状态",
                width: 70,
                align: "center"
              },
              {
                prop: "statusName",
                label: "单据状态",
                width: 100,
                align: "center"
              },
              {
                prop: "crruNodeName",
                label: "作业状态",
                width: 120,
                align: "center"
              },
              {
                prop: "createdDate",
                label: "建单时间",
                width: 150,
                align: "center"
              },
              {
                prop: "remainDataDate",
                label: "留资时间",
                width: 150,
                align: "center"
              },
              {
                prop: "isFirstCallbackName",
                label: "是否跟踪",
                width: 80,
                align: "center"
              },
              {
                prop: "firstReviewDate",
                label: "首次回访时间",
                width: 150,
                align: "center"
              },
              {
                prop: "isOneHour",
                label: "是否超过1H",
                width: 80,
                align: "center"
              },
              {
                prop: "isFourHour",
                label: "是否超过4H",
                width: 80,
                align: "center"
              },
              {
                prop: "isTwentyFourHour",
                label: "是否超过24H",
                width: 80,
                align: "center"
              },
              {
                prop: "orderDate",
                label: "订单时间",
                width: 150,
                align: "center"
              },
              {
                prop: "deliveryDate",
                label: "交车时间",
                width: 150,
                align: "center"
              },
              {
                prop: "nextCallbackDate",
                label: "下次回访时间/预约到店时间",
                width: 250,
                align: "center"
              },
              {
                prop: "changeIntentDate",
                label: "转意向时间",
                width: 150,
                align: "center"
              },
              {
                prop: "nextDealEmpName",
                label: "销售顾问",
                width: 100,
                align: "center"
              },
              {
                prop: "inteSeriesCode",
                label: "意向车系编码",
                width: 120,
                align: "center",
                hidden: true
              }
            ],
      formField: {
        custName: "",
        contactTel: "",
        inteSeriesId: "",
        clueLevelCode: "",
        // createdDateStart: "",
        // createdDateEnd: "",
        createdDateStart:that.getStartDate() + " 00:00:00",
        createdDateEnd:that.getEndDate() + " 00:00:00",
        "createdDateStart,createdDateEnd": [
          that.getStartDate() + " 00:00:00",
          that.getEndDate() + " 00:00:00"
        ],
        orderDateStart: "",
        orderDateEnd: "",
        status: "",
        csr: "",
        serverOrder: "",
        cantactWayCode: "",
        infoChanMCode: "",
        carBrandCode: "",
        assignStatus: "",
        crruNode: "",
        orderDateStart: "",
        orderDateEnd: "",
        createdName: "",
        dlrCode: "",
        handleCode: "1",
        assignStatusName: "",
        statusArray: ["10", "20", "21", "30", "31", "40", "50"],
        remainDataDateStart: "",
        remainDataDateEnd: "",
        nextDealUserId: "",
        nextDealUserName: "",
        infoChanDName: "",
        isQueryHistory:"0",
      },
      inteSeriesId: "",
      input: "",
      width: "900px",
      heigh: "580px",
      isQueryHistory:"0",
      dropLists:{}
    };
  },
  computed: {
    visitedViews() {
      return this.$store.state.tagsView.visitedViews;
    }
  },
  mounted() {
    //判断销售顾问
    console.log(this.$store.getters.orgInfo.empName);
    console.log(this.$store.getters.orgInfo.POS_CODE);
    if (this.$store.getters.orgInfo.POS_CODE.indexOf("VE0003") > -1||this.$store.getters.orgInfo.POS_CODE.indexOf("COC_XS_CSR_HEADER") > -1) {
      this.formField.nextDealUserId = "";
    } else {
      this.formField.nextDealUserId = this.$store.getters.orgInfo.userId;
    }
    this.getDropLists();
  },
  methods: {
    query() {
      console.log(111);

      if (this.$store.getters.orgInfo.POS_CODE.indexOf("VE0003") > -1||this.$store.getters.orgInfo.POS_CODE.indexOf("COC_XS_CSR_HEADER") > -1) {
        this.formField.nextDealUserId = "";
      } else {
        this.formField.nextDealUserId = this.$store.getters.orgInfo.userId;
      }
      this.formField.isQueryHistory=this.isQueryHistory
      // this.queryTable(1);
      let obj = {};
      obj.formField = JSON.parse(JSON.stringify(this.formField));
      this.$delete(obj.formField, "createdDateStart,createdDateEnd");
      this.queryTable(1, null, null, null, null, obj);
    },
    resetcur() {
      if(this.isQueryHistory=='0'){
        this.formField["createdDateStart,createdDateEnd"] = [
          this.getStartDate() + " 00:00:00",
          this.getEndDate() + " 00:00:00"
        ];
      }
      this.reset();
      let patt = new RegExp(","),
        that = this;
      for (let key in that.$refs.multipleTable.$refs) {
        if (
          that.$refs.multipleTable.$refs[key][0] &&
          that.$refs.multipleTable.$refs[key][0].codeField
        ) {
          let flag = patt.test(
            that.$refs.multipleTable.$refs[key][0].codeField
          );
          if (flag) {
            that.$refs.multipleTable.$refs[key][0].modelCode = [];
          }
        }
      }
      console.log(this.formField, 123123123);

      if (this.$store.getters.orgInfo.POS_CODE.indexOf("VE0003") > -1||this.$store.getters.orgInfo.POS_CODE.indexOf("COC_XS_CSR_HEADER") > -1) {
        this.formField.nextDealUserId = "";
      } else {
        this.formField.nextDealUserId = this.$store.getters.orgInfo.userId;
      }

    },
    getStartDate(){
      let dd = new Date();
      dd.setMonth(dd.getMonth()-1);
      let y = dd.getFullYear();
      let m = dd.getMonth()+1;
      let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
      return y + "-" + (m < 10 ? "0" + m : m) + "-" + d;
    },
    getEndDate(){
      let dd = new Date();
      let y = dd.getFullYear();
      let m = dd.getMonth() + 1;
      let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
      return y + "-" + (m < 10 ? "0" + m : m) + "-" + d;
    },
     changeToggleProp(){
      this.resetcur()
    },
    inteSeriesChange(row) {
      debugger;
      this.formField.inteSeriesId = row[0].carSeriesId;
    },
    //导出
    export(){
      let obj = {};
      obj.formField = JSON.parse(JSON.stringify(this.formField));
      this.$delete(obj.formField, "createdDateStart,createdDateEnd");
      debugger
      this.$nextTick(function(){
        this.exportExcel(
          "线索单查询" + this.$config.excelExp,
          "线索单查询",
          -1,
          obj,
          this.dropLists
        )
      })

    },
    //获取导出模板下拉值
    getDropLists(){
      let that = this;
      let queryObj = {
        // api配置
        apiConfig: crmApis.sourceChannelInfoQueryChoose,
        // 需要调用的API服务配置
        apiServices: [
          {
            //表格中台返回网格的字段
            apiQueryRow: [
              'infoChanDName'
            ]
          }
        ],
        //条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          pageSize: -1,
          pageIndex: 1,
          //当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: {
            cantactWayId: "CS002"
          }
        }
      };
      //转换了中台请求格式数据
      var paramD = that.$getParams(queryObj);
      // 调用中台API方法（可复用）
      requestAPI(paramD).then(response => {
        if (response.result == "1") {
          let results = response.rows;
          this.dropLists.infoChanDName=[]
          for(let i=0;i<results.length;i++){
            this.dropLists.infoChanDName.push(results[i].infoChanDName)
          }
        }
      });
    },
    // 跳转线索单导入
    importExcel: function() {
      this.$crmcf.jumpDetailTag(this, "ClueOrderImport", "/ClueOrderImport");
    },
    handle: function(index, rowData) {
      let that = this;
      this.$crmcf.jumpDetailTag(that, "ClueDetail", "/ClueDetail", rowData);
    },
    edit(rowData) {
      rowData.isQueryHistory=this.isQueryHistory
      this.$crmcf.jumpDetailTag(this, "ClueDetail", "/ClueDetail", rowData);
    },
    getHistory(){
      let that = this;
      if(that.$refs.multipleTable.tableButtons[2].type){
        that.formField.isQueryHistory='0'
        that.$refs.multipleTable.tableButtons[2].type = "";
        that.formField.createdDateStart=that.getStartDate();
        that.formField.createdDateEnd=that.getEndDate();
        that.formField["createdDateStart,createdDateEnd"] = [
          that.getStartDate() + " 00:00:00",
          that.getEndDate() + " 00:00:00"
        ];
      }else{
        that.formField.isQueryHistory='1'
        that.$refs.multipleTable.tableButtons[2].type = "primary";
        that.formField.createdDateStart='';
        that.formField.createdDateEnd='';
        that.formField["createdDateStart,createdDateEnd"] = [
          '',
          ''
        ];
      }
      that.isQueryHistory=that.formField.isQueryHistory
      that.query()
    },
    //线索渠道change
    cantactWayChange(){
     this.formField.infoChanMCode=''
    },
  }
};
</script>

